#include "mypushbutton.h"
#include "qdebug.h"
#include <qpushbutton.h>
#include <QPropertyAnimation>

MyPushButton::MyPushButton(QString normalImg,QString pressImg){
    this->normalImg = normalImg;
    this->pressImg = pressImg;

    QPixmap pix;
    bool ret = pix.load(normalImg);
    if (!ret){
        qDebug() << "图片加载失败";
        return;
    }

    this->setFixedSize(pix.width(),pix.height());

    // 设置不规则图片样式
    this->setStyleSheet("QPushButton{border:none;}");

    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

}

void MyPushButton::zoomDown(){
    QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry");
    // 设置动画时间
    animation->setDuration(200);
    // 设置起点
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    // 设置终点
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));

    // 设置速率
    animation->setEasingCurve(QEasingCurve::OutBounce);
    // 动画开始
    animation->start();
}
void MyPushButton::zoomUp(){
    QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry");
    // 设置动画时间
    animation->setDuration(200);
    // 设置起点
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    // 设置终点
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));

    // 设置速率
    animation->setEasingCurve(QEasingCurve::OutBounce);
    // 动画开始
    animation->start();
}

void MyPushButton::mousePressEvent(QMouseEvent *e){
    if(this->pressImg != ""){
        // 切换图片
        QPixmap pix;
        bool ret = pix.load(this->pressImg);
        if (!ret){
            qDebug() << "图片加载失败";
            return;
        }

        this->setFixedSize(pix.width(),pix.height());

        // 设置不规则图片样式
        this->setStyleSheet("QPushButton{border:0px;}");

        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mousePressEvent(e);
}
void MyPushButton::mouseReleaseEvent(QMouseEvent *e){

    if(this->pressImg != ""){
        // 切换图片
        QPixmap pix;
        bool ret = pix.load(this->normalImg);
        if (!ret){
            qDebug() << "图片加载失败";
            return;
        }

        this->setFixedSize(pix.width(),pix.height());

        // 设置不规则图片样式、透明背景
        this->setStyleSheet("QPushButton{border:0px;background: transparent;}");

        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mouseReleaseEvent(e);
}


